package com.SmartCare.service.impl.moments;

import com.SmartCare.common.dto.moments.UpdateCommentInfoDTO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.SmartCare.common.Result;
import com.SmartCare.common.dto.moments.CommentInfoDTO;
import com.SmartCare.mapper.momments.CommentInfoMapper;
import com.SmartCare.pojo.moments.CommentInfo;
import com.SmartCare.service.moments.CommentInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class CommentInfoServiceImpl extends ServiceImpl<CommentInfoMapper, CommentInfo> implements CommentInfoService {
    //评论动态
    @Override
    public Result commentDynamic(CommentInfoDTO comment) {
        if (comment.getContent().isEmpty()){
            return Result.error("请输入评论内容");
        }
        CommentInfo info = new CommentInfo();
        BeanUtils.copyProperties(comment,info);
        boolean res = save(info);
        if (res){
            LambdaQueryWrapper<CommentInfo> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(CommentInfo::getUserId, comment.getUserId());
            wrapper.eq(CommentInfo::getDynamicId, comment.getDynamicId());
            wrapper.eq(CommentInfo::getContent, comment.getContent());
            CommentInfo one = getOne(wrapper);

            Map<String ,Long> map=new HashMap<>();
            map.put("id",one.getId());
            return Result.success("评论成功",map);
        }
        return Result.error("评论失败");
    }

    //修改评论
    @Override
    public Result updateComment(UpdateCommentInfoDTO comment) {
        LambdaUpdateWrapper<CommentInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(CommentInfo::getId, comment.getId());
        wrapper.set(CommentInfo::getContent,comment.getContent());
        boolean res = update(wrapper);
        if (res){
            return Result.success("修改评论成功");
        }
        return Result.success("修改评论失败");
    }

    @Override
    public Result deleteComment(Long id) {
        boolean res = removeById(id);
        if(res){
            return Result.success("删除评论成功");
        }
        return Result.error("删除评论失败");
    }

}
